﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Db4objects.Db4o;
using System.Linq;

namespace Yosca
{
	/// <summary>
	/// Interaction logic for CustProfile.xaml
	/// </summary>
	public partial class CustProfile : Window
	{
        Invoice inv;
		Database dbase = Database.Instance;
		public CustProfile(Invoice loaded)
		{
			this.InitializeComponent();
            inv = loaded;
            
			// Insert code required on object creation below this point.
            Customer cust = inv.defCust;
            updateFields(cust);
			
		}

		private void editCustClick(object sender, System.Windows.RoutedEventArgs e)
		{
			setAllEditsVisible();
			setAllEditsToFields();
			saveButton.Visibility = Visibility.Visible;
		}
		private void setAllEditsVisible()
		{
			fName.Visibility = Visibility.Collapsed;
			lName.Visibility = Visibility.Collapsed;
			bAddress.Visibility = Visibility.Collapsed;
			bCity.Visibility = Visibility.Collapsed;
			bState.Visibility = Visibility.Collapsed;
			bZip.Visibility = Visibility.Collapsed;
		
			hPhone.Visibility = Visibility.Collapsed;
			cPhone.Visibility = Visibility.Collapsed;
			email.Visibility = Visibility.Collapsed;
			website.Visibility = Visibility.Collapsed;
	
			
			fNameBox.Visibility = Visibility.Visible;
			lNameBox.Visibility = Visibility.Visible;
			bAddrBox.Visibility = Visibility.Visible;
			cityBox.Visibility = Visibility.Visible;
			bStateCombo.Visibility = Visibility.Visible;
			zipBox.Visibility = Visibility.Visible;
			homePhoneBox.Visibility = Visibility.Visible;
			cellPhoneBox.Visibility = Visibility.Visible;
			emailBox.Visibility = Visibility.Visible;
			webBox.Visibility = Visibility.Visible;
		
			
			// black box on background
			box1.Visibility = Visibility.Collapsed;
			box2.Visibility = Visibility.Collapsed;
			box3.Visibility = Visibility.Collapsed;
			box4.Visibility = Visibility.Collapsed;
			box5.Visibility = Visibility.Collapsed;
			box6.Visibility = Visibility.Collapsed;
			box7.Visibility = Visibility.Collapsed;
			box8.Visibility = Visibility.Collapsed;
			box9.Visibility = Visibility.Collapsed;
			box10.Visibility = Visibility.Collapsed;
			/*
			box11.Visibility = Visibility.Collapsed;
			box12.Visibility = Visibility.Collapsed;
			box13.Visibility = Visibility.Collapsed;
			box14.Visibility = Visibility.Collapsed;
			box15.Visibility = Visibility.Collapsed;
			*/
			
		}
        private void setAllLabelsVisible()
        {
            fName.Visibility = Visibility.Visible;
            lName.Visibility = Visibility.Visible;
            invoiceNum.Visibility = Visibility.Visible;
            bAddress.Visibility = Visibility.Visible;
            bCity.Visibility = Visibility.Visible;
            bState.Visibility = Visibility.Visible;
            bZip.Visibility = Visibility.Visible;
     
            hPhone.Visibility = Visibility.Visible;
            cPhone.Visibility = Visibility.Visible;
            email.Visibility = Visibility.Visible;
            website.Visibility = Visibility.Visible;
        

            fNameBox.Visibility = Visibility.Collapsed;
            lNameBox.Visibility = Visibility.Collapsed;
            bAddrBox.Visibility = Visibility.Collapsed;
            cityBox.Visibility = Visibility.Collapsed;
            bStateCombo.Visibility = Visibility.Collapsed;
            zipBox.Visibility = Visibility.Collapsed;
            homePhoneBox.Visibility = Visibility.Collapsed;
            cellPhoneBox.Visibility = Visibility.Collapsed;
            emailBox.Visibility = Visibility.Collapsed;
            webBox.Visibility = Visibility.Collapsed;
      
			// black box on background
			box1.Visibility = Visibility.Visible;
			box2.Visibility = Visibility.Visible;
			box3.Visibility = Visibility.Visible;
			box4.Visibility = Visibility.Visible;
			box5.Visibility = Visibility.Visible;
			box6.Visibility = Visibility.Visible;
			box7.Visibility = Visibility.Visible;
			box8.Visibility = Visibility.Visible;
			box9.Visibility = Visibility.Visible;
			box10.Visibility = Visibility.Visible;
			/*
			box11.Visibility = Visibility.Visible;
			box12.Visibility = Visibility.Visible;
			box13.Visibility = Visibility.Visible;
			box14.Visibility = Visibility.Visible;
			box15.Visibility = Visibility.Visible;
			*/

        }
		private void setAllEditsToFields()
		{
			fNameBox.Text = fName.Content.ToString();
			lNameBox.Text = lName.Content.ToString();
			bAddrBox.Text = bAddress.Content.ToString();
			cityBox.Text = bCity.Content.ToString();
			
			zipBox.Text = bZip.Content.ToString();
			homePhoneBox.Text = hPhone.Content.ToString();
			cellPhoneBox.Text = cPhone.Content.ToString();
			emailBox.Text = email.Content.ToString();
			webBox.Text = website.Content.ToString();
			
		}

		private void closeClick(object sender, System.Windows.RoutedEventArgs e)
		{
			saveButton.Visibility = Visibility.Collapsed;
			this.Close();
		}

		private void gMapClick(object sender, System.Windows.RoutedEventArgs e)
		{
			string target = "http://www.maps.google.com/?q=";
			string input = jAddress.Text + " " + jCity.Text + " " + jZip.Text;
			target += input;
		    try{
         		System.Diagnostics.Process.Start(target);
			}
    		catch(System.ComponentModel.Win32Exception noBrowser){
         		if (noBrowser.ErrorCode == -2147467259)
          		    MessageBox.Show(noBrowser.Message);
        	}
    		catch (System.Exception other)
        	{
          		MessageBox.Show(other.Message);
        	}
		}

		private void saveClick(object sender, System.Windows.RoutedEventArgs e)
		{
            inv.defCust.name = fNameBox.Text.ToUpper() + " " + lNameBox.Text.ToUpper();
            inv.defCust.bAddress = bAddrBox.Text.ToUpper();
            inv.defCust.bCity = cityBox.Text.ToUpper();
            inv.defCust.bState = bStateCombo.Text.ToUpper();
            inv.defCust.bZip = zipBox.Text.ToUpper();

            inv.defCust.hPhone = homePhoneBox.Text.ToUpper();
            inv.defCust.cPhone = cellPhoneBox.Text.ToUpper();
            inv.defCust.website = webBox.Text.ToUpper();
            inv.defCust.email = emailBox.Text.ToUpper();

            dbase.db.Store(inv.defCust);
			dbase.db.Store(inv);
            dbase.db.Commit();
            saveButton.Visibility = Visibility.Collapsed;
            updateFields(inv.defCust);
            setAllLabelsVisible();
		}
        private void updateFields(Customer cust)
        {
			
			if(inv.invoiceStatus > 0){
				newEstButton.Visibility = Visibility.Collapsed;
			    invButton.Visibility = Visibility.Visible;			
			}
			if(inv.jAddress == "" || inv.jCity == "" || inv.jZip == ""){
					editJobButton.Visibility = Visibility.Collapsed;
					saveJobButton.Visibility = Visibility.Visible;
					
					jAddrLbl.Visibility = Visibility.Collapsed;
					jCityLbl.Visibility = Visibility.Collapsed;
					jStateLbl.Visibility = Visibility.Collapsed;
					jZipLbl.Visibility = Visibility.Collapsed;
				    noteLblViewer.Visibility = Visibility.Collapsed;
					
					redFlags.Visibility = Visibility.Visible;
					jAddress.Visibility = Visibility.Visible;
					jCity.Visibility = Visibility.Visible;
					jState.Visibility = Visibility.Visible;
					jZip.Visibility = Visibility.Visible;
					noteTextViewer.Visibility = Visibility.Visible;
			}
			
			else{	
					jAddrLbl.Content = inv.jAddress;
					jCityLbl.Content = inv.jCity;
					jStateLbl.Content = inv.jState;
					jZipLbl.Content = inv.jZip;
					notesLbl.Text = inv.notes;
			}
			
            Customer.Content = cust.name.ToUpper();
            string[] fAndL = cust.name.Split(' ');
            fName.Content = fAndL[0];
            lName.Content = fAndL[1];
            invoiceNum.Content = inv.inv_num;
            bAddress.Content = cust.bAddress;
            bCity.Content = cust.bCity;
            bState.Content = cust.bState;
            bZip.Content = cust.bZip;
            
            hPhone.Content = cust.hPhone;
            cPhone.Content = cust.cPhone;
            email.Content = cust.email;
            website.Content = cust.website;

            result.Content = inv.getInvoiceStatus();
			
        }

        private void newEstCLick(object sender, System.Windows.RoutedEventArgs e)
        {
			if (MessageBox.Show("Start an estimate for " + inv.defCust.name + "?","Invoice", MessageBoxButton.YesNo) == MessageBoxResult.Yes){
				inv.invoiceStatus = 1;   // set invoice status to Estimate
				inv.estimateDate = DateTime.Now.Date; // set estimate date

				dbase.db.Store(inv.defCust); 
				dbase.db.Store(inv);         
				dbase.db.Commit();
		
        		InvoiceProfile newInv = new InvoiceProfile(inv);
				this.Close();
				newInv.Show();
			}
        }

        private void InvClick(object sender, System.Windows.RoutedEventArgs e)
        {
			
        	InvoiceProfile newInv = new InvoiceProfile(inv);
			newInv.Show();
			this.Close();
			
        }

        private void photoClick(object sender, System.Windows.RoutedEventArgs e)
        {
        	PhotoManager photoView = new PhotoManager(inv);
			photoView.Show();
        }

        private void saveJobClick(object sender, System.Windows.RoutedEventArgs e)
        {
        	inv.jAddress = jAddress.Text.ToUpper();
            inv.jCity = jCity.Text.ToUpper();
            inv.jState = jState.Text.ToUpper();
            inv.jZip = jZip.Text.ToUpper();
		    inv.notes = noteBox.Text.ToUpper();
		
			dbase.db.Store(inv);
			dbase.db.Commit();
		
			jAddress.Visibility = Visibility.Collapsed;
			jCity.Visibility = Visibility.Collapsed;
			jState.Visibility = Visibility.Collapsed;
			jZip.Visibility = Visibility.Collapsed;
			noteTextViewer.Visibility = Visibility.Collapsed;
			saveJobButton.Visibility = Visibility.Collapsed;
			editJobButton.Visibility = Visibility.Visible;
			redFlags.Visibility = Visibility.Collapsed;
			jAddrLbl.Content = inv.jAddress;
			jCityLbl.Content = inv.jCity;
			jStateLbl.Content = inv.jState;
			jZipLbl.Content = inv.jZip;
			notesLbl.Text = inv.notes;
			jAddrLbl.Visibility = Visibility.Visible;
			jCityLbl.Visibility = Visibility.Visible;
			jStateLbl.Visibility = Visibility.Visible;
			jZipLbl.Visibility = Visibility.Visible;
			noteLblViewer.Visibility = Visibility.Visible;
			
			
        }

        private void editJobClick(object sender, System.Windows.RoutedEventArgs e)
        {
			saveJobButton.Visibility = Visibility.Visible;
			editJobButton.Visibility = Visibility.Collapsed;
       

			jAddress.Visibility = Visibility.Visible;
			jCity.Visibility = Visibility.Visible;
			jState.Visibility = Visibility.Visible;
			jZip.Visibility = Visibility.Visible;
			noteTextViewer.Visibility = Visibility.Visible;
			saveJobButton.Visibility = Visibility.Visible;
			redFlags.Visibility = Visibility.Visible;
			jAddrLbl.Visibility = Visibility.Collapsed;
			jCityLbl.Visibility = Visibility.Collapsed;
			jStateLbl.Visibility = Visibility.Collapsed;
			jZipLbl.Visibility = Visibility.Collapsed;
			noteLblViewer.Visibility = Visibility.Collapsed;
			
			jAddress.Text = inv.jAddress;
			jCity.Text = inv.jCity;
			jState.SelectedItem = inv.jState;
			jZip.Text = inv.jZip;
			noteBox.Text = inv.notes;
			
        }
		

       

	}
}